﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>

<script type="text/javascript">
    <%=ViewData["Provinces"] %>
</script>
<%
    var city = ViewData["ColName"] ?? "CityID";
    var fild = city.ToString().Replace('.', '_');
    var prov = fild+"_ProvinceID";
%>
<script type="text/javascript">
        var <%=fild%>_provname = '<%=ViewData["provid"] %>';
        var <%=fild%>_cityname = '<%=ViewData["cityid"] %>';
        var <%=fild%>_ddlprov = '#<%=prov %>';
        var <%=fild%>_ddlcity = '#<%=fild%>';

        function <%=fild%>_checkProvince(args, val) {
            val.IsValid = $(<%=fild%>_ddlprov)[0].value != "0";
            return val.IsValid;  
        } 
        function <%=fild%>_checkCity(args, val) {
            val.IsValid = $(<%=fild%>_ddlcity)[0].value != "0";
            return val.IsValid;
        }

        function <%=fild%>_bindCity(val) {
            var city = $(<%=fild%>_ddlcity);
            if(val.value=='-省份-')
            return;
            city[0].options.length = 0;
            var citys =Provinces[val.value].Citys;
            for (var item in citys) {
                var str='';
                if (citys[item].ID == <%=fild%>_cityname) {
                    str="  selected='selected' ";
                }
                if (citys[item].Name == "-城市-") {
                    city.append("<option value "+str+">" + citys[item].Name + "</option>");
                } else {
                    city.append("<option value='" + citys[item].ID + "' "+str+">" + citys[item].Name + "</option>");
                }
            }
            city[0].style.width="auto";
        }

        function <%=fild%>_bindProvince() {
            var prov = $(<%=fild%>_ddlprov);
            prov[0].options.length = 0;
            for (var item in Provinces) {
                var str='';
                if (Provinces[item].ID == <%=fild%>_provname) {
                    str=" selected='selected' ";
                }
                if(Provinces[item].ID==""){
                    prov.append("<option value=''>" + Provinces[item].Name + "</option>");
                }else{
                    prov.append("<option value='" + Provinces[item].ID  + "' "+str+">" + Provinces[item].Name + "</option>");
                }
            }
  
            <%=fild%>_bindCity(prov[0]);
        }
    
        $(document).ready(function(){
            <%=fild%>_bindProvince();
//            var prov = $(<%=fild%>_ddlprov);
//            var city = $(<%=fild%>_ddlcity);
//            if(<%=fild%>_provname!=""&&<%=fild%>_cityname!=""){
//                prov.val(<%=fild%>_provname);
//                <%=fild%>_bindCity(prov[0]);
//                city.val(<%=fild%>_cityname);
//            }
        });
    
</script>
<select id='<%=prov %>' name='<%=prov %>' onchange="<%=fild%>_bindCity(this)" style="width: auto">
    <option value="选择省份">选择省份</option>
</select>省<select id='<%=fild%>' name='<%=city%>' style="width: auto">
    <option value="">选择城市</option>
</select>市/区
